package com.vehicle.biz.Impl;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.vehicle.biz.SysRoleBiz;
import com.vehicle.domains.entity.SysPermissionEntity;
import com.vehicle.domains.entity.SysRoleEntity;
import com.vehicle.domains.entity.SysRolePermissionEntity;
import com.vehicle.domains.vo.BasePage;
import com.vehicle.domains.bo.QueryPermissionBo;
import com.vehicle.domains.bo.QueryRoleBo;
import com.vehicle.domains.vo.req.sysRole.AddRolePermissionsReq;
import com.vehicle.domains.vo.req.sysRole.AddRoleReq;
import com.vehicle.domains.vo.req.sysRole.ListRoleReq;
import com.vehicle.domains.vo.req.sysRole.UpdateRoleReq;
import com.vehicle.domains.vo.res.sysRole.SysRoleRes;
import com.vehicle.enums.PermissionTypeEnum;
import com.vehicle.exception.VehicleAccessException;
import com.vehicle.service.SysPermissionService;
import com.vehicle.service.SysRolePermissionService;
import com.vehicle.service.SysRoleService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class SysRoleBizImpl implements SysRoleBiz {

    @Resource
    private SysRoleService sysRoleService;
    @Resource
    private SysRolePermissionService sysRolePermissionService;
    @Resource
    private SysPermissionService sysPermissionService;

    @Override
    public void addRole(AddRoleReq req) {
        SysRoleEntity sysRoleEntity = new SysRoleEntity();
        BeanUtils.copyProperties(req, sysRoleEntity);
        sysRoleService.addRole(sysRoleEntity);
    }

    @Override
    public void updateRole(UpdateRoleReq req) {
        checkRoleExist(req.getId());
        SysRoleEntity sysRoleEntity = new SysRoleEntity();
        BeanUtils.copyProperties(req, sysRoleEntity);
        sysRoleService.updateRole(sysRoleEntity);
    }

    @Override
    public void deleteRole(Long id) {
        checkRoleExist(id);
        sysRoleService.deleteRole(id);
    }

    @Override
    public SysRoleRes getRoleById(Long id) {
        SysRoleEntity sysRoleEntity = checkRoleExist(id);
        SysRoleRes sysRoleRes = new SysRoleRes();
        BeanUtils.copyProperties(sysRoleEntity, sysRoleRes);
        return sysRoleRes;
    }

    @Override
    public BasePage<SysRoleRes> selectRoleList(ListRoleReq req) {
        Page<SysRoleEntity> page = PageHelper.startPage(req.getPageNo(), req.getPageSize());
        QueryRoleBo queryRoleBo = new QueryRoleBo();
        BeanUtils.copyProperties(req, queryRoleBo);
        List<SysRoleEntity> sysRoleEntityList= sysRoleService.selectRoleList(queryRoleBo);
        List<SysRoleRes> sysRoleResList = sysRoleEntityList.stream().map(sysRoleEntity -> {
            SysRoleRes sysRoleRes = new SysRoleRes();
            BeanUtils.copyProperties(sysRoleEntity, sysRoleRes);
            return sysRoleRes;
        }).toList();
        return BasePage.generateResult(page.getTotal(), page.getPages(), sysRoleResList);
    }

    @Override
    public void addRolePermissions(AddRolePermissionsReq req) {
        Long roleId = req.getRoleId();
        //判断角色是否存在
        checkRoleExist(roleId);

        if (CollectionUtil.isEmpty(req.getPermissionIds())) {
            sysRolePermissionService.addRolePermissions(roleId,null);
            return;
        }

        QueryPermissionBo permissionBo = new QueryPermissionBo();
        permissionBo.setIds(req.getPermissionIds());
        permissionBo.setStatusList(Collections.singletonList(PermissionTypeEnum.BUTTON.name()));

        //权限合法校验
        List<SysPermissionEntity> sysPermissionEntityList = sysPermissionService.selectPermissionList(permissionBo);
        if (CollectionUtil.isEmpty(sysPermissionEntityList) || sysPermissionEntityList.size() != req.getPermissionIds().size()) {
            throw VehicleAccessException.DATA_NOT_EXIST.newInstance("有权限id不存在");
        }

        List<SysRolePermissionEntity> list = new ArrayList<>();

        for (SysPermissionEntity sysPermissionEntity : sysPermissionEntityList) {
            SysRolePermissionEntity sysRolePermissionEntity = new SysRolePermissionEntity();
            sysRolePermissionEntity.setRoleId(roleId);
            sysRolePermissionEntity.setPermissionId(sysPermissionEntity.getId());
            list.add(sysRolePermissionEntity);
        }

        //删除旧的权限，添加新的权限
        sysRolePermissionService.addRolePermissions(roleId,list);

    }

    private SysRoleEntity checkRoleExist(Long id) {
        SysRoleEntity sysRoleEntity=sysRoleService.selectByRoleId(id);
        if (sysRoleEntity==null){
            throw VehicleAccessException.DATA_NOT_EXIST.newInstance("角色不存在");
        }
        return sysRoleEntity;
    }
}
